﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



using System.ServiceModel;
using BPM.ClientLink.pws;
using BPM.Foundation.PWDB;

namespace BPM.Base.WFLib
{
	public partial class DepartmentDutyForm: Form
	{
        public string Value;
		public DepartmentDutyForm()
		{
			InitializeComponent();
		}

        private void DepartmentDutyForm_Load(object sender, EventArgs e)
        {
            FillDepartmentTree();
        }

        public void FillDepartmentTree()
        {
            this.treDepartment.Nodes.Clear();
           DepartmentTree dTree = pws.getPWS() .getDepartmentTree("", "System");

           TreeNode root = new TreeNode();
           root.Tag = dTree.current;
          
           root.Text = dTree.current.departmentName;
           this.treDepartment.Nodes.Add(root);

           AddDepartmentTreeToTreeView(root, dTree);
        }

        private void AddDepartmentTreeToTreeView(TreeNode parentNode, DepartmentTree dTree)
        {
            foreach (var obj in dTree.childList)
            {

                TreeNode node = new TreeNode();
                node.Text = obj.current.departmentName;
                node.Tag = obj.current;
                parentNode.Nodes.Add(node);

                AddDepartmentTreeToTreeView(node, obj);
            }
        }

        private void treDepartment_AfterSelect(object sender, TreeViewEventArgs e)
        {
             PW_Department dTree=e.Node.Tag  as PW_Department;

             // MessageBox.Show(e.Node.Tag);
             if (dTree != null)
             {
                 //MessageBox.Show(dTree.current.departmentNo);
                 List<PW_Duty> listDuty = pws.getPWS().getDutyFromDepartment("", dTree.departmentNo).ToList();
                 FillDuty(listDuty);
             }
            
        }


        private void FillDuty(List<PW_Duty> list)
        {
            this.lstDutyInDepartment.DataSource = null;
            this.lstDutyInDepartment.DataSource = list;
            this.lstDutyInDepartment.DisplayMember = "dutyName";
            this.lstDutyInDepartment.ValueMember = "dutyNo";
            //this.lstDutyInDepartment.
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //this.Value=this.treDepartment.SelectedNode.ta
            
            if(this.treDepartment.SelectedNode!=null && this.lstDutyInDepartment.SelectedItem!=null )
            {
                PW_Department p = this.treDepartment.SelectedNode.Tag as PW_Department;
                PW_Duty duty = this.lstDutyInDepartment.SelectedItem as PW_Duty;
                //this.Value = p.departmentNo + "," + p.departmentName;
                this.Value = string.Format("{0},{1}|{2},{3}", p.departmentNo, p.departmentName,duty.dutyNo,duty.dutyName);
                this.Hide();
            }

        }

       
	}
}
